package com.coc.serviceimpl.system;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coc.core.utils.StringRelaUtils;
import com.coc.dao.system.SysRoleDao;
import com.coc.entity.core.JsonResult;
import com.coc.entity.core.ResultTool;
import com.coc.entity.system.SysRole;
import com.coc.entity.system.SysRolePermission;
import com.coc.entity.system.SysUser;
import com.coc.service.system.SysRoleService;
import com.coc.service.system.SysRolePermissionService;
import com.coc.service.system.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户角色表(SysRole)表服务实现类
 *
 * @author songju
 * @since 2020-02-10 19:12:42
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    @Autowired
    private SysUserService sysUserService;
    @Transactional
    @Override
    public JsonResult addUser(String json) {
        String permitIds = JSONObject.parseObject(json).getString("permitIds");
        SysRole sysRole = JSONObject.parseObject(json, SysRole.class);
        sysRole.setInsertTime(new Date());
        boolean save = save(sysRole);
        if (save){
            QueryWrapper<SysRole> sqw = new QueryWrapper<>();
            sqw.eq("code",sysRole.getCode());
            SysRole one = getOne(sqw);
            SysRolePermission sp=null;
            if (StrUtil.isNotBlank(permitIds)&&permitIds.length()==1){
                sp=new SysRolePermission();
                sp.setPermitId(Integer.parseInt(permitIds));
                sp.setRoleId(one.getId());
                sysRolePermissionService.saveOrUpdate(sp);
            }else if (StrUtil.isNotBlank(permitIds)&&permitIds.length()>1){
                ArrayList<SysRolePermission> sur = new ArrayList<>();
                List<String> rls = StringRelaUtils.strToList(permitIds);
                for (String rl:rls){
                    sp=new SysRolePermission();
                    sp.setRoleId(one.getId());
                    sp.setPermitId(Integer.parseInt(rl));
                    sur.add(sp);
                }
                sysRolePermissionService.saveBatch(sur);
            }
        }

        return ResultTool.success();
    }
    @Transactional
    @Override
    public JsonResult editRole(String json) {
        SysRole sysRole = JSONObject.parseObject(json, SysRole.class);
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUser sysUser = sysUserService.GetCurrUser();
        sysRole.setInsertUid(sysUser.getId());
        sysRole.setUpdateTime(new Date());
        updateById(sysRole);
        String permitIds = jsonObject.getString("permitIds");
        //将该角色对应的权限删除后再重新插入
        QueryWrapper<SysRolePermission> spw = new QueryWrapper<>();
        spw.eq("role_id",sysRole.getId());
        sysRolePermissionService.remove(spw);
        SysRolePermission sp=null;
        if (StrUtil.isNotBlank(permitIds)){
                if (permitIds.length()==1){
                    SysRolePermission srp = new SysRolePermission();
                    srp.setRoleId(sysRole.getId());
                    srp.setPermitId(Integer.parseInt(permitIds));
                    sysRolePermissionService.save(srp);
                }else {
                    ArrayList<SysRolePermission> sur = new ArrayList<>();
                    List<String> rls = StringRelaUtils.strToList(permitIds);
                    for (String rl:rls){
                        sp=new SysRolePermission();
                        sp.setRoleId(sysRole.getId());
                        sp.setPermitId(Integer.parseInt(rl));
                        sur.add(sp);
                    }
                    sysRolePermissionService.saveBatch(sur);
                }
        }
        return ResultTool.success();
    }

}